/*
    Priscilla - A library for enocean network access
    Copyright (C) 2012-2013 B. Aigner / R. Wagner

    This program is free software; you can redistribute it and/or 
    modify it under the terms of the GNU General Public License 
    as published by the Free Software Foundation; either version 2 
    of the License, or at your option any later version. 
 
    This program is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    GNU General Public License for more details. 
 
    You should have received a copy of the GNU General Public License 
    along with this program; if not, write to the Free Software 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
 
    Linking this library statically or dynamically with other modules is 
    making a combined work based on this library. Thus, the terms and 
    conditions of the GNU General Public License cover the whole 
    combination. 
 
    As a special exception, the copyright holders of this library give you 
    permission to link this library with independent modules to produce an 
    executable, regardless of the license terms of these independent 
    modules, and to copy and distribute the resulting executable under terms 
    of your choice, provided that you also meet, for each linked independent 
    module, the terms and conditions of the license of that module. An 
    independent module is a module which is not derived from or based on 
    this library. If you modify this library, you may extend this exception 
    to your version of the library, but you are not obligated to do so. If 
    you do not wish to do so, delete this exception statement from your 
    version. 
*/

package at.technikum_wien.embsys.aat.PriscillaCore.parsing;

import at.technikum_wien.embsys.aat.PriscillaCore.data.EnoceanRawData;
import at.technikum_wien.embsys.aat.PriscillaCore.exception.EnOceanExceptionDeviceUnknown;
import at.technikum_wien.embsys.aat.PriscillaCore.exception.EnOceanExceptionFormat;
import at.technikum_wien.embsys.aat.PriscillaCore.exception.EnOceanExceptionLenght;

/**
 * This class checks the format of an IP frame received by the Enocean/IP gateway
 * 
 * @author R. Wagner
 * @version 1.0
 */
public class EnoceanIPData {
	
	EnoceanRawData dataFrame;
	/**
	 * Size of an Enocean IP frame, used for checking the received length.
	 * If the length does not match, an EnOceanExceptionLength is thrown.
	 */
	private static final int HEADER_SIZE = 28;
	/**
	 * Sync string of an Enocean IP frame, used for checking the received frame.
	 * If the sync string does not match, an EnOceanExceptionFormat is thrown.
	 */
	private static final String HEADERSTRING = "A55A";
	
	/**
	 * Create a new IP header class with a given data array
	 * 
	 * By calling this constructor, the length and the sync bytes are checked.
	 * If something does not match, a corresponding exception is thrown.
	 * 
	 * @param frame Character array of the received data
	 * @throws EnOceanExceptionFormat If the sync bytes does not match
	 * @throws EnOceanExceptionLenght If the length does not match
	 * @throws EnOceanExceptionDeviceUnknown
	 */
	public EnoceanIPData(char[] frame) throws EnOceanExceptionDeviceUnknown, EnOceanExceptionFormat, EnOceanExceptionLenght{

		if (frame.length != HEADER_SIZE) //check for frame length
			throw new EnOceanExceptionLenght("EnOcean IP-Lenght does not fit");

		//check for sync string
		if (!String.copyValueOf(frame, 0, 4).equals(HEADERSTRING)) {
			throw new EnOceanExceptionLenght("EnOcean IP-Header does not match");
		}
				
	}
}
